//
//  JDImproveUserNameView.swift
//  DragonWallet
//
//  Created by volantPig on 2024/4/25.
//

import UIKit
/// 完善用户名
class JDImproveUserNameView: UIView {
    
    var containerView: UIView?
    var cancelButton: UIButton?
    var confirmButton: UIButton?
    var userNameInputView: UITextField?
    var tipInfoLabel: JDPaddingLabel?
    var titleLabel: UILabel?
    
    var titleStr: String? {
        didSet {
            self.tipInfoLabel?.text = self.titleStr
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: CGRectMake(0, 0, screenWidth, screenHeight))
        self.setupConfig()
        self.setupUIElements()
    }
    
    func setupConfig() {
        self.backgroundColor = color("#000000").withAlphaComponent(0.4)
    }
    
    func setupUIElements() {
        self.containerView = UIView()
        self.addSubview(self.containerView!)
        self.containerView?.snp.makeConstraints({ make in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(305)
        })
        self.containerView?.backgroundColor = .white
        
        self.cancelButton = UIButton(type: .custom)
        self.containerView?.addSubview(self.cancelButton!)
        self.cancelButton?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!.snp.centerX).offset(-5)
            make.bottom.equalTo(self.containerView!).offset(-25)
            make.height.equalTo(54)
        })
        self.cancelButton?.layer.cornerRadius = 27
        self.cancelButton?.backgroundColor = color("#F5F6F8")
        self.cancelButton?.setTitle("取消", for: .normal)
        self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.cancelButton?.setTitleColor(.black, for: .normal)
        self.cancelButton?.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        
        self.confirmButton = UIButton(type: .custom)
        self.containerView?.addSubview(self.confirmButton!)
        self.confirmButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerView!).offset(-12)
            make.left.equalTo(self.containerView!.snp.centerX).offset(5)
            make.bottom.equalTo(self.containerView!).offset(-25)
            make.height.equalTo(54)
        })
        self.confirmButton?.setTitle("确定", for: .normal)
        self.confirmButton?.layer.cornerRadius = 27
        self.confirmButton?.backgroundColor = color("#333333")
        self.confirmButton?.setTitleColor(.white, for: .normal)
        self.confirmButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.confirmButton?.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
        
        self.userNameInputView = UITextField()
        self.containerView?.addSubview(self.userNameInputView!)
        self.userNameInputView?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
            make.bottom.equalTo(self.cancelButton!.snp.top).offset(-49)
            make.height.equalTo(58)
        })
        self.userNameInputView?.layer.cornerRadius = 8
        self.userNameInputView?.backgroundColor = color("#F5F6F8")
        self.userNameInputView?.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        self.userNameInputView?.textColor = color("#333333")
        self.userNameInputView?.placeholder = "请输入用户名"
        
        let leftView = JDPaddingLabel()
        leftView.edgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 20)
        leftView.text = "用户名"
        leftView.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 14, weight: .medium), textColor: color("#333333"), numberOfLines: 1)
        
        self.userNameInputView?.leftViewMode = .always
        self.userNameInputView?.leftView = leftView
        
    
        self.tipInfoLabel = JDPaddingLabel()
        self.tipInfoLabel?.edgeInsets = UIEdgeInsets(top: 6, left: 15, bottom: 6, right: 15)
        self.containerView?.addSubview(self.tipInfoLabel!)
        self.tipInfoLabel?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.containerView!)
            make.bottom.equalTo(self.userNameInputView!.snp.top).offset(-26)
            make.height.equalTo(30)
        })
        self.tipInfoLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 13, weight: .regular), textColor: color("#FA6161"), numberOfLines: 0)
        self.tipInfoLabel?.layer.cornerRadius = 15
        self.tipInfoLabel?.clipsToBounds = true
        self.tipInfoLabel?.backgroundColor = color("#FFF9F9")
        self.tipInfoLabel?.text = "首次发布的用户请先完善您的用户名"
        
        self.titleLabel = UILabel()
        self.containerView?.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.containerView!)
            make.height.equalTo(25)
            make.bottom.equalTo(self.tipInfoLabel!.snp.top).offset(-8)
        })
        self.titleLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 18, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        self.titleLabel?.text = "完善用户名"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func cancelAction() {
        self.removeFromSuperview()
    }
    
    @objc func confirmAction() {
        self.cancelAction()
        printLog("确定")
    }
    
}
